function TestPageHTML() {	
}

function olderBrowsersXhttpRequest() {}
 
TestPageHTML.prototype.setExceptions =
function(noOfExceptions) {
	getElementByID('exceptions').innerHTML = noOfExceptions;
}

TestPageHTML.prototype.setFails =
function (noOfFails) {
	getElementByID('fails').innerHTML = noOfFails;
}

TestPageHTML.prototype.setPasses =
function (noOfPasses) {
	getElementByID('passes').innerHTML = noOfPasses;
} 

TestPageHTML.prototype.setTotalTestCases =
function (total) {
	getElementByID('total').innerHTML = total;
}

TestPageHTML.prototype.setCompletedTestCases =
function (testCases) {
	getElementByID('completed').innerHTML = testCases;
}

TestPageHTML.prototype.getCompletedTestCases =
function () {
	return Number(getElementByID('completed').innerHTML);
}

TestPageHTML.prototype.setTotalNoOfTests =
function (total) {
	getElementByID('total').innerHTML = total;
}

TestPageHTML.prototype.getTotalNoOfTests =
function () {
	return Number(getElementByID('total').innerHTML);
}

TestPageHTML.prototype.addNewTestHTML =
function(noOfTest, testName, testOutputList) {
	var test = document.createElement(SINGLE_TEST_ELEMENT);
	var span = document.createElement('span');
	test.setAttribute('id', 'testNo-' + noOfTest );
	span.setAttribute('onclick', "TestRunner.refreshCase('" + noOfTest + "');");
	span.appendChild( document.createTextNode( testName ) );
	test.appendChild(span);
	
	this.addGroupNodeToList(test, testOutputList);
}

TestPageHTML.prototype.addNodeForGroup =
function(groupName, testOutputList) {
	var groupHeading = testOutputList.createElement(GROUP_HEADING_ELEMENT);
	groupHeading.appendChild( testOutputList.createTextNode(groupName) );
	
	var groupContainer = testOutputList.createElement(GROUP_CONTAINER_ELEMENT);
	groupContainer.setAttribute(TEST_GROUP_ATTRIBUTE, TEST_GROUP_ID);
	groupContainer.appendChild(groupHeading);
	
	return groupContainer;
}

TestPageHTML.prototype.addGroupNodeToList =
function(testGroup, testOutputList) {
	if ( testOutputList == null )
		getElementByID(TEST_SUITE_CONTAINTER_ID).appendChild(testGroup);
	else 
		testOutputList.appendChild(testGroup);
}

TestPageHTML.prototype.addNewTestGroupHTML =
function(groupName, testOutputList) {
	var testGroup = this.addNodeForGroup(groupName, document);
	var groupOfTestsElement = document.createElement(GROUP_OF_TESTS_ELEMENT);
	testGroup.appendChild(groupOfTestsElement);
	
	this.addGroupNodeToList(testGroup, testOutputList);
	
	return groupOfTestsElement;
}

TestPageHTML.prototype.setTestStatusToLoading =
function(testNo) {
	getElementByID('testNo-' + testNo).style.color = STATUS_TEST_UNTESTED;	
}

TestPageHTML.prototype.setTestStatusToCompleted =
function(testNo) {
	getElementByID('testNo-' + testNo).style.color = STATUS_TEST_COMPLETED;
}

TestPageHTML.prototype.getTestStatus =
function(testNo) {
	return getElementByID('testNo-' + testNo).style.color;
}

TestPageHTML.prototype.checkPluralAndAdjustStyle = 
function(cssStyleId) {
	if ( getNoOf(cssStyleId) == 1 )
		getElementByID(cssStyleId + '_plural').style.display = 'none';
	else
		getElementByID(cssStyleId + '_plural').style.display = '';
}

TestPageHTML.prototype.removeTestResultMessage = 
function(noOfTest) {
	messages = getElementByID('messages').getElementsByTagName('div');
			
	for ( i = 0; i < messages.length; i++ )	{
		if ( messages[i].getAttribute('noOfTest') == noOfTest )
			getElementByID('messages').removeChild(messages[i]);	
	}
}

TestPageHTML.prototype.updateTotalStatus =
function(type, updateAmount) {
	if ( type == 'passes'){
		getElementByID('passes').innerHTML = getNoOf('passes') + updateAmount;	
		this.checkPluralAndAdjustStyle('passes');
	}
	
	if ( type == 'Fail' ) {
		getElementByID('fails').innerHTML = getNoOf('fails') + updateAmount;
		this.checkPluralAndAdjustStyle('fails');
	}
		
	if ( type == 'Exception' ) {
		getElementByID('exceptions').innerHTML = getNoOf('exceptions') + updateAmount;	
		this.checkPluralAndAdjustStyle('exceptions');
	}	
}

TestPageHTML.prototype.updateTestStatus =
function(test, type, updateAmount) {
	if ( type == 'Fail' ) 
		test.fails = test.fails + updateAmount;
		
	if ( type == 'Exception' )
		test.exceptions = test.exceptions + updateAmount;	
}

TestPageHTML.prototype.updateTestResuts = 
function(test, passes, fails, exceptions, messages) {
	
	this.updateTotalStatus('passes', Number(passes) - Number(test.passes));	
	
	test.passes = passes;
	
	this.setFails(getNoOf('fails') - Number(test.fails));
	this.setExceptions(getNoOf('exceptions') - Number(test.exceptions));
	
	test.fails = 0;
	test.exceptions = 0;	
	
		
	for ( msgNo = 0; msgNo < messages.length; msgNo++ )
	{	
		if(messages[msgNo].getAttribute('type') == "Fail")
			this.addNewFailMessage(test.noOfTest, getTestMsgData(messages[msgNo], "location"), getTestMsgData( messages[msgNo], "output"), "Fail");

		if(messages[msgNo].getAttribute('type') == "Exception")
			this.addNewExceptionMessage(test.noOfTest, getTestMsgData(messages[msgNo], "location"), getTestMsgData( messages[msgNo], "output"), "Exception");	

		this.updateTestStatus(test, messages[msgNo].getAttribute('type') , 1);
		this.updateTotalStatus(messages[msgNo].getAttribute('type'), 1);
	}
	
}

function getTestMsgData( htmlElement, tag)
{
	var elems = htmlElement.getElementsByTagName(tag);

	return elems[0].firstChild.data;
}

TestPageHTML.prototype.addNewPassMessage = 
function(testNo, location, message, testMsgType){
	
	var msg = document.createElement(MESSAGE_ELEMENT);		
	msg.setAttribute('noOfTest', testNo);
	
	var testResult = createMsgTypeElement(testMsgType);
	
	testResult.appendChild(createHTMLElement('location', location));
	testResult.appendChild(createHTMLElement('output', message));	
	msg.appendChild(testResult);
		
	getElementByID('messages').appendChild(msg);
}

TestPageHTML.prototype.addNewFailMessage = 
function(testNo, location, message, testMsgType){
	
	var msg = document.createElement(MESSAGE_ELEMENT);		
	msg.setAttribute('noOfTest', testNo);
	
	var testResult = createMsgTypeElement(testMsgType);
	
	testResult.appendChild(createHTMLElement('location', location));
	testResult.appendChild(createHTMLElement('output', message));	
	msg.appendChild(testResult);
		
	getElementByID('messages').appendChild(msg);
}

TestPageHTML.prototype.addNewExceptionMessage = 
function(testNo, location, message, testMsgType){
	
	var msg = document.createElement(MESSAGE_ELEMENT);		
	msg.setAttribute('noOfTest', testNo);
	
	var testResult = createMsgTypeElement(testMsgType);
	
	testResult.appendChild(createHTMLElement('location', location));
	testResult.appendChild(createHTMLElement('output', message));	
	msg.appendChild(testResult);
		
	getElementByID('messages').appendChild(msg);
}

function createHTMLElement(label, data) {
	
	var element = document.createElement(MESSAGE_TYPE_ELEMENT);
	var element_data = document.createElement(MESSAGE_TYPE_ELEMENT);
	var element_label = document.createElement(MESSAGE_TYPE_ELEMENT);
	
	element_label.appendChild(document.createTextNode(label + ': '));
	element_label.setAttribute('class', 'label');		
	element_data.appendChild(document.createTextNode(data));
	element_data.setAttribute('class', 'data');		
	element.appendChild(element_label);
	element.appendChild(element_data);
	element.setAttribute('class', label);
	
	return element;
}

function createMsgTypeElement(type){	
	
	var msgType = document.createElement(MESSAGE_TYPE_ELEMENT);
	var msgType_label = document.createElement(MESSAGE_TYPE_ELEMENT);

	msgType.setAttribute('class', type.toLowerCase());	
	msgType_label.appendChild( document.createTextNode(type) );
	msgType_label.setAttribute('class', 'label');
	msgType.appendChild(msgType_label);

	return msgType;
}

function getNoOf(elementID) {
	return Number(getElementByID(elementID).innerHTML);
}

function refreshTestResultBG(fails, exceptions) {

	if ( fails + exceptions > 0 )
		getElementByID('test-results').style.backgroundColor = 'red';
	else
		getElementByID('test-results').style.backgroundColor = 'green';
}

function getElementByID(elementID) {
	return document.getElementById(elementID);
}

/*function getElementByID(htmlDoc, elementID) 
{
	return htmlDoc.getElementById(elementID);
}*/